import { defineStore } from 'pinia'
import { getHexagramList, getHexagramDetail, createHexagram, updateHexagram, deleteHexagram, getAlgorithmList, getAlgorithmDetail, createAlgorithm, updateAlgorithm, deleteAlgorithm } from '@/api/model'

export const useModelStore = defineStore('model', {
  state: () => ({
    hexagramList: [],
    hexagramDetail: {},
    algorithmList: [],
    algorithmDetail: {},
    loading: false,
    total: 0
  }),

  actions: {
    // 获取卦象列表
    async fetchHexagramList(params) {
      this.loading = true
      try {
        const response = await getHexagramList(params)
        this.hexagramList = response.data
        this.total = response.total
        return response
      } catch (error) {
        console.error('获取卦象列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取卦象详情
    async fetchHexagramDetail(id) {
      this.loading = true
      try {
        const response = await getHexagramDetail(id)
        this.hexagramDetail = response.data
        return response
      } catch (error) {
        console.error('获取卦象详情失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建卦象
    async createHexagram(data) {
      try {
        const response = await createHexagram(data)
        // 如果当前在卦象列表页，重新获取列表
        if (this.hexagramList.length > 0) {
          await this.fetchHexagramList({})
        }
        return response
      } catch (error) {
        console.error('创建卦象失败:', error)
        throw error
      }
    },

    // 更新卦象
    async updateHexagram(id, data) {
      try {
        const response = await updateHexagram(id, data)
        // 更新卦象列表中的数据
        const index = this.hexagramList.findIndex(hexagram => hexagram.id === id)
        if (index !== -1) {
          this.hexagramList[index] = { ...this.hexagramList[index], ...data }
        }
        // 如果是当前详情页的卦象，也更新详情数据
        if (this.hexagramDetail.id === id) {
          this.hexagramDetail = { ...this.hexagramDetail, ...data }
        }
        return response
      } catch (error) {
        console.error('更新卦象失败:', error)
        throw error
      }
    },

    // 删除卦象
    async deleteHexagram(id) {
      try {
        const response = await deleteHexagram(id)
        // 从卦象列表中移除
        this.hexagramList = this.hexagramList.filter(hexagram => hexagram.id !== id)
        this.total--
        return response
      } catch (error) {
        console.error('删除卦象失败:', error)
        throw error
      }
    },

    // 获取算法列表
    async fetchAlgorithmList(params) {
      this.loading = true
      try {
        const response = await getAlgorithmList(params)
        this.algorithmList = response.data
        this.total = response.total
        return response
      } catch (error) {
        console.error('获取算法列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取算法详情
    async fetchAlgorithmDetail(id) {
      this.loading = true
      try {
        const response = await getAlgorithmDetail(id)
        this.algorithmDetail = response.data
        return response
      } catch (error) {
        console.error('获取算法详情失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建算法
    async createAlgorithm(data) {
      try {
        const response = await createAlgorithm(data)
        // 如果当前在算法列表页，重新获取列表
        if (this.algorithmList.length > 0) {
          await this.fetchAlgorithmList({})
        }
        return response
      } catch (error) {
        console.error('创建算法失败:', error)
        throw error
      }
    },

    // 更新算法
    async updateAlgorithm(id, data) {
      try {
        const response = await updateAlgorithm(id, data)
        // 更新算法列表中的数据
        const index = this.algorithmList.findIndex(algorithm => algorithm.id === id)
        if (index !== -1) {
          this.algorithmList[index] = { ...this.algorithmList[index], ...data }
        }
        // 如果是当前详情页的算法，也更新详情数据
        if (this.algorithmDetail.id === id) {
          this.algorithmDetail = { ...this.algorithmDetail, ...data }
        }
        return response
      } catch (error) {
        console.error('更新算法失败:', error)
        throw error
      }
    },

    // 删除算法
    async deleteAlgorithm(id) {
      try {
        const response = await deleteAlgorithm(id)
        // 从算法列表中移除
        this.algorithmList = this.algorithmList.filter(algorithm => algorithm.id !== id)
        this.total--
        return response
      } catch (error) {
        console.error('删除算法失败:', error)
        throw error
      }
    }
  }
})